package InterviewPractise;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * Created by IntelliJ IDEA.
 * User: Richard
 * Date: 5/20/11
 * Time: 11:03 AM
 * To change this template use File | Settings | File Templates.
 */
public class StringReplace {
    public static void main(String[] args){
        String str = "qwe\\qweqwe\\\\";
        System.out.println(str);
        String str2 = str.replaceAll("\\\\","/");
        System.out.println(str2);
        System.out.println(Arrays.toString(permutation("abc")));

    }


    private static String[] permutation(String orginal){
        ArrayList<String> list = new ArrayList<String>();
        if(orginal.length() == 1){
            return new String[]{orginal};
        } else{
           for (int i = 0; i < orginal.length(); i++) {
             String s = orginal.charAt(i) + "";
             String result = "";
             String resultA = result + s;
             String leftS = orginal.substring(0, i) + orginal.substring(i + 1, orginal.length());
             for (String element : permutation(leftS)) {
                    result = resultA + element;
                    list.add(result);
                }
            }
            return (String[]) list.toArray(new String[list.size()]);
        }
    }

    private static String[] allCollection(String org){
        ArrayList<String> list = new  ArrayList<String>();
        if(org.length()==1){
            return new String[] {org};
        }
        else{
            for(int i =0 ; i < org.length() ; i++){
                String s = org.charAt(i)+"";
                String result = "";
                String resultA = result + s;
                String leftS = org.substring(0, i) + org.substring(i + 1, org.length());
                for(String element : allCollection(leftS)){
                    result = s + leftS;
                    list.add(result);
                }

            }
            return (String[]) list.toArray(new String[list.size()]);
        }
    }
}
